package cn.good.yan.basics.t3;

import cn.good.yan.basics.t2.pojo.Bill;
import cn.good.yan.basics.t3.pojo.Provider;
import cn.good.yan.basics.t3.service.ProviderService;
import cn.good.yan.basics.t3.service.ProviderService2;
import cn.good.yan.basics.t3.utils.RedisUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;


/**
 * 测试 ：缓存Redis
 *     引入相关包：
 *     <dependency>
 *         <groupId>org.springframework.boot</groupId>
 *         <artifactId>spring-boot-starter-data-redis</artifactId>
 *     </dependency>
 * @author shengren.yan
 * @create 2022-11-24
 */
@SpringBootTest
public class testRedis {

    //针对 的都是操作字符串
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    // 人人框架提供的Redis工具类
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 缓存Redis
     * 五大数据类型
     *         stringRedisTemplate.opsForValue();//操作字符串
     *         stringRedisTemplate.opsForList();//操作List
     *         stringRedisTemplate.opsForSet();//操作Set
     *         stringRedisTemplate.opsForZSet();//操作ZSet
     *         stringRedisTemplate.opsForHash();//操作Hash
     * @throws Exception
     */
    @Test
    void test1() throws Exception{
        // 设置缓存
        stringRedisTemplate.opsForValue().set("name", "yan");
        // 读取缓存
        String name = stringRedisTemplate.opsForValue().get("name");
        System.out.println(name);
    }


    // 人人提供的工具类 需要 RedisConfig 配置   RedisUtils 工具类
    @Test
    void test2() throws Exception{
        Bill bill = new Bill();
        bill.setBillName("qqq@qq.com");
        bill.setBillCode("222SSSs是");
        bill.setBid(3L);
        // 设置
        redisUtils.set("bill", bill);
        // 获取
        Bill bill1 = redisUtils.get("bill", Bill.class);
        System.out.println(ToStringBuilder.reflectionToString(redisUtils.get("bill", Bill.class)));
    }


    @Autowired(required = false)
    private ProviderService providerService;

    // 增删改 - 保存Redis
    @Test
    void test3() throws Exception{
        Provider provider = new Provider();
        provider.setPid(1);
        provider.setProviderCode("北京");
        provider.setFax("faxcs");
        // 新增
//        providerService.addProvider(provider);

        // 获取
        Provider providerByPid = providerService.getProviderByPid(1);
        System.out.println(providerByPid);

        // 修改
        provider.setFax("xxx");
        providerService.updateProvider(provider);

        // 删除
        providerService.deleteProviderByPid(1);
    }

    @Autowired(required = false)
    private ProviderService2 providerService2;
    // 注解版 - 增删改 - 保存Redis
    @Test
    void test4() throws Exception{
        Provider provider = new Provider();
        provider.setPid(1);
        provider.setProviderCode("北京");
        provider.setFax("faxcs");
        // 新增
//        providerService2.addById(5);

        // 获取
        Provider providerByPid = providerService2.getByPid(5);
        System.out.println(providerByPid);
//
//        // 修改
//        provider.setFax("xxx");
        providerService2.updateUser(provider);
//
//        // 删除
        providerService2.deleteUserById(5);
    }

}


